package com.macro.mall.ums.service.account.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.common.api.util.StringUtils;
import com.macro.mall.ums.domain.WxMember;
import com.macro.mall.ums.entity.WxMemberEntity;
import com.macro.mall.ums.mapper.WxMemberMapper;
import com.macro.mall.ums.service.account.WxMemberService;
import com.macro.mall.ums.service.account.bo.InsertWxMemberInput;
import com.macro.mall.ums.service.account.bo.PagedQueryWxMemberInput;
import com.macro.mall.ums.service.account.bo.UpdateWxMemberInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员微信Service
 *
 * @author xuyanjun
 * @date 2019-12-08
 */
@Slf4j
@Service
public class WxMemberServiceImpl implements WxMemberService {

    @Autowired
    private WxMemberMapper wxMemberMapper;

    /**
     * 新增会员微信
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertWxMemberInput input) {
        WxMemberEntity wxMemberEntity = new WxMemberEntity();
        wxMemberEntity
                .setMemberId(input.getMemberId())
                .setWxOpenId(input.getWxOpenId())
                .setWxUnionId(input.getWxUnionId())
                .setWxHeadImgUrl(input.getWxHeadImgUrl())
                .setWxNickname(input.getWxNickname())
                .setWxMobileNo(input.getWxMobileNo())
                .setWxNo(input.getWxNo())
                .setCreateIp(input.getCreateIp())
                .setWxProvince(input.getWxProvince())
                .setWxCity(input.getWxCity())
                .setWxCountry(input.getWxCountry())
                .setDeleteStatus(0)
                .setSourceType(input.getSourceType().getValue());

        LocalDateTime now = LocalDateTime.now();
        wxMemberEntity.setCreateTime(now);
        wxMemberEntity.setUpdateTime(now);

        wxMemberMapper.insert(wxMemberEntity);
        return wxMemberEntity.getId();
    }

    /**
     * 修改会员微信
     *
     * @param input input
     */
    @Override
    public void update(UpdateWxMemberInput input) {
        WxMemberEntity wxMemberEntity = wxMemberMapper.selectById(input.getId());
        if (wxMemberEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的会员微信。", input.getId()));
        }

        wxMemberEntity = new WxMemberEntity();
        wxMemberEntity
                .setId(input.getId())
                .setMemberId(input.getMemberId())
                .setWxOpenId(input.getWxOpenId())
                .setWxUnionId(input.getWxUnionId())
                .setWxHeadImgUrl(input.getWxHeadImgUrl())
                .setWxNickname(input.getWxNickname())
                .setWxMobileNo(input.getWxMobileNo())
                .setWxNo(input.getWxNo())
                .setCreateIp(input.getCreateIp())
                .setWxProvince(input.getWxProvince())
                .setWxCity(input.getWxCity())
                .setWxCountry(input.getWxCountry());

        LocalDateTime now = LocalDateTime.now();
        wxMemberEntity.setUpdateTime(now);

        wxMemberMapper.updateById(wxMemberEntity);
    }

    /**
     * 删除会员微信
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        wxMemberMapper.deleteById(id);
    }

    /**
     * 根据Id获取会员微信
     *
     * @param id id
     * @return 会员微信
     */
    @Override
    public WxMemberEntity getById(long id) {
        return wxMemberMapper.selectById(id);
    }

    /**
     * 根据用户id获取用户微信
     *
     * @param memberId 用户id
     * @return 用户微信
     */
    @SuppressWarnings("Duplicates")
    @Override
    public WxMemberEntity getByMemberId(long memberId) {
        LambdaQueryWrapper<WxMemberEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WxMemberEntity::getMemberId, memberId);

        List<WxMemberEntity> wxUserEntities = wxMemberMapper.selectList(queryWrapper);
        if (!wxUserEntities.isEmpty()) {
            return wxUserEntities.get(0);
        }

        return null;
    }

    /**
     * 根据openId获取用户微信
     *
     * @param openId openId
     * @return 用户微信
     */
    @SuppressWarnings("Duplicates")
    @Override
    public WxMemberEntity getByOpenId(String openId) {
        Assert.isTrue(StringUtils.isNotBlank(openId), "openId不能为空。");

        LambdaQueryWrapper<WxMemberEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WxMemberEntity::getWxOpenId, openId);

        List<WxMemberEntity> wxUserEntities = wxMemberMapper.selectList(queryWrapper);
        if (!wxUserEntities.isEmpty()) {
            return wxUserEntities.get(0);
        }

        return null;
    }

    /**
     * 根据unionId获取用户微信
     *
     * @param unionId unionId
     * @return 用户微信
     */
    @SuppressWarnings("Duplicates")
    @Override
    public WxMemberEntity getByUnionId(String unionId) {
        Assert.isTrue(StrUtil.isNotBlank(unionId), "unionId不能为空。");

        LambdaQueryWrapper<WxMemberEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WxMemberEntity::getWxUnionId, unionId);

        List<WxMemberEntity> wxUserEntities = wxMemberMapper.selectList(queryWrapper);
        if (!wxUserEntities.isEmpty()) {
            return wxUserEntities.get(0);
        }

        return null;
    }

    /**
     * 根据批量Id获取会员微信
     *
     * @param batchIds 批量ids
     * @return 会员微信集合
     */
    @Override
    public List<WxMemberEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return wxMemberMapper.selectBatchIds(batchIds);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的会员微信
     */
    @Override
    public Page<WxMemberEntity> pagedQuery(PagedQueryWxMemberInput input) {
        LambdaQueryWrapper<WxMemberEntity> queryWrapper = Wrappers.lambdaQuery();

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<WxMemberEntity>) wxMemberMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param wxMemberEntity 实体
     * @return wxMember
     */
    @Override
    public WxMember entityToDomain(WxMemberEntity wxMemberEntity) {
        if (wxMemberEntity == null) {
            return null;
        }

        WxMember wxMember = new WxMember();
        wxMember
                .setId(wxMemberEntity.getId())
                .setCreateTime(wxMemberEntity.getCreateTime())
                .setUpdateTime(wxMemberEntity.getUpdateTime())
                .setMemberId(wxMemberEntity.getMemberId())
                .setWxOpenId(wxMemberEntity.getWxOpenId())
                .setWxUnionId(wxMemberEntity.getWxUnionId())
                .setWxHeadImgUrl(wxMemberEntity.getWxHeadImgUrl())
                .setWxNickname(wxMemberEntity.getWxNickname())
                .setWxMobileNo(wxMemberEntity.getWxMobileNo())
                .setWxNo(wxMemberEntity.getWxNo())
                .setCreateIp(wxMemberEntity.getCreateIp())
                .setWxProvince(wxMemberEntity.getWxProvince())
                .setWxCity(wxMemberEntity.getWxCity())
                .setWxCountry(wxMemberEntity.getWxCountry())
                .setSourceType(wxMemberEntity.getSourceType());

        return wxMember;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param wxMemberEntities 实体
     * @return wxMembers
     */
    @Override
    public List<WxMember> entityToDomain(List<WxMemberEntity> wxMemberEntities) {
        List<WxMember> result = new ArrayList<>();
        if (wxMemberEntities == null) {
            return result;
        }

        for (WxMemberEntity wxMemberEntity : wxMemberEntities) {
            WxMember wxMember = entityToDomain(wxMemberEntity);
            result.add(wxMember);
        }
        return result;
    }

}
